//
//  TestStringVC.m
//  技术点总结
//
//  Created by tianlong on 2018/6/7.
//  Copyright © 2018年 tianlong. All rights reserved.
//

#import "TestStringVC.h"

@interface TestStringVC ()
@property (copy, nonatomic) NSString *str1_copy;
@property (copy, nonatomic) NSString *str2_copy;
@property (copy, nonatomic) NSString *str3_copy;

@property (strong, nonatomic) NSString *str1_Strong;
@property (strong, nonatomic) NSString *str2_Strong;
@property (strong, nonatomic) NSString *str3_Strong;


@property (copy, nonatomic) NSString *str_copy;
@property (strong, nonatomic) NSString *str_strong;
/** 模拟外界传递的可变字符串 */
//@property (strong, nonatomic) NSMutableString *content;
@property (strong, nonatomic) NSString *content;
@end

@implementation TestStringVC

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.content        = [[NSString alloc] initWithString:@"456"];
//    self.content        = [NSString stringWithFormat:@"123"];
    NSString *str1 = self.content;
    NSString *str2 = [self.content copy];
    NSString *str3 = [self.content mutableCopy];
    self.str_copy       = self.content;
    self.str_strong     = self.content;
    
    [(NSMutableString *)str3 appendString:@"+++hhhh"];
    
    NSLog(@"\ncontent     = %p（源）\nstr1        = %p（等号）\nstr2        = %p（copy）\nstr3        = %p（mutableCopy）\nstr_copy    = %p（byCopy）\nstr_strong  = %p（byStrong）",self.content,str1,str2,str3,self.str_copy,self.str_strong);
    NSLog(@"\ncontent     = %@\nstr1        = %@\nstr2        = %@\nstr3        = %@\nstr_copy    = %@\nstr_strong  = %@",self.content,str1,str2,str3,self.str_copy,self.str_strong);
    
    
    
    /**
     content     = 0xa000000003332313（源）
     str1        = 0xa000000003332313（等号）
     str2        = 0xa000000003332313（copy）
     str3        = 0x600000442b50（mutableCopy）
     str_copy    = 0xa000000003332313（byCopy）
     str_strong  = 0xa000000003332313（byStrong）
     
     不可变
     content     = 0x103227968（源）
     str1        = 0x103227968（等号）
     str2        = 0x103227968（copy）
     str3        = 0x60000045a160（mutableCopy）
     str_copy    = 0x103227968（byCopy）
     str_strong  = 0x103227968（byStrong）
     
     可变
     content     = 0x604000252060（源）
     str1        = 0x604000252060（等号）
     str2        = 0xa000000003635343（copy）
     str3        = 0x600000050470（mutableCopy）
     str_copy    = 0xa000000003635343（byCopy）
     str_strong  = 0x604000252060（byStrong）
     
     结论：
     不可变字符串
     等号赋值，copy赋值，copy修饰，strong修饰均是浅拷贝（指针拷贝）
     mutableCopy赋值是深拷贝，并且生成的新字符串是可变字符串
     
     可变字符串
     等号赋值，strong修饰是浅拷贝（指针拷贝）
     copy赋值，mutableCopy赋值，copy修饰都是深拷贝
     mutableCopy赋值生成的新字符串为可变字符串
     */
}

#pragma mark - 不可变
- (IBAction)testImmutableString:(id)sender {
    
    NSString *str = @"testI";
    
    self.str1_copy = str;
    self.str2_copy = [str copy];
    self.str3_copy = [str mutableCopy];
    
    self.str1_Strong = str;
    self.str2_Strong = [str copy];
    self.str3_Strong = [str mutableCopy];
    
    NSLog(@"内存地址： \nstr         = %p\nstr1_copy   = %p \nstr2_copy   = %p \nstr3_copy   = %p\nstr1_Strong = %p \nstr2_Strong = %p \nstr3_Strong = %p",str,self.str1_copy,self.str2_copy,self.str3_copy,self.str1_Strong,self.str2_Strong,self.str3_Strong);
    NSLog(@"值： \nstr         = %@\nstr1_copy   = %@ \nstr2_copy   = %@ \nstr3_copy   = %@\nstr1_Strong = %@ \nstr2_Strong = %@ \nstr3_Strong = %@",str,self.str1_copy,self.str2_copy,self.str3_copy,self.str1_Strong,self.str2_Strong,self.str3_Strong);
    NSLog(@"真实类型：\nstr1_copy_class = %@ \nstr2_copy_class = %@ \nstr3_copy_class = %@,\nstr1_strong_class = %@ \nstr2_strong_class = %@ \nstr3_strong_class = %@",NSStringFromClass([self.str1_copy class]),NSStringFromClass([self.str2_copy class]),NSStringFromClass([self.str3_copy class]),NSStringFromClass([self.str1_Strong class]),NSStringFromClass([self.str2_Strong class]),NSStringFromClass([self.str3_Strong class]));
    
    /**
     str         = 0x102097958
     
     str1_copy   = 0x102097958
     str2_copy   = 0x102097958 
     str3_copy   = 0xa000049747365745
     
     str1_Strong = 0x102097958
     str2_Strong = 0x102097958
     str3_Strong = 0x604000254cd0
     
     结论：
     由copy修饰的不可变字符串
     在进行等号，copy赋值时，都是浅拷贝，没有生成新的对象
     在进行mutableCopy时，深拷贝，生成了新的对象
    
     由strong修饰的不可变字符串
     在进行等号，copy赋值时，都是浅拷贝，没有生成新的对象
     在进行mutableCopy时，深拷贝，生成了新的对象
     */
    
}

#pragma mark - 可变
- (IBAction)testMutableString:(id)sender {
    NSMutableString *str = [[NSMutableString alloc] initWithString:@"testM"];
    
    self.str1_copy = str;
    self.str2_copy = [str copy];
    self.str3_copy = [str mutableCopy];
    
    self.str1_Strong = str;
    self.str2_Strong = [str copy];
    self.str3_Strong = [str mutableCopy];
    
    [str appendString:@"++hello"];
    
    NSLog(@"内存地址： \nstr         = %p\nstr1_copy   = %p \nstr2_copy   = %p \nstr3_copy   = %p\nstr1_Strong = %p \nstr2_Strong = %p \nstr3_Strong = %p",str,self.str1_copy,self.str2_copy,self.str3_copy,self.str1_Strong,self.str2_Strong,self.str3_Strong);
    NSLog(@"值： \nstr         = %@\nstr1_copy   = %@ \nstr2_copy   = %@ \nstr3_copy   = %@\nstr1_Strong = %@ \nstr2_Strong = %@ \nstr3_Strong = %@",str,self.str1_copy,self.str2_copy,self.str3_copy,self.str1_Strong,self.str2_Strong,self.str3_Strong);
    NSLog(@"真实类型：\nstr1_copy_class = %@ \nstr2_copy_class = %@ \nstr3_copy_class = %@,\nstr1_strong_class = %@ \nstr2_strong_class = %@ \nstr3_strong_class = %@",NSStringFromClass([self.str1_copy class]),NSStringFromClass([self.str2_copy class]),NSStringFromClass([self.str3_copy class]),NSStringFromClass([self.str1_Strong class]),NSStringFromClass([self.str2_Strong class]),NSStringFromClass([self.str3_Strong class]));
    /**
     str         = 0x60000045ecf0
     
     str1_copy   = 0xa00004d747365745
     str2_copy   = 0xa00004d747365745
     str3_copy   = 0xa00004d747365745
     
     str1_Strong = 0x60000045ecf0
     str2_Strong = 0xa00004d747365745
     str3_Strong = 0x600000642520
     
     结论
     被copy修饰的可变字符串
     无轮是等号，copy还是mutableCopy赋值，都是深拷贝
     比较有趣的是：等号，copy和mutableCopy出来的新对象是同一个
     
     被strong修饰的可变字符串
     等号赋值：浅拷贝，没有生成新的对象
     copy和mutableCopy 深拷贝，生成的是新对象
     */
}

- (IBAction)changeString:(id)sender {
    if ([self.content isKindOfClass:[NSMutableString class]]) {
        [self.content performSelector:@selector(appendString:) withObject:@"++这是什么鬼"];
    }
    NSLog(@"\ncontent     = %@\nstr_copy    = %@\nstr_strong  = %@",self.content,self.str_copy,self.str_strong);
}



@end
